package keeto.com.vn.bms.ui.fragment;

import java.util.ArrayList;
import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.database.BMSManager;
import keeto.com.vn.bms.model.BMSUserModel;
import keeto.com.vn.bms.model.InfoMessegerModel;
import keeto.com.vn.bms.model.InfoReportModel;
import keeto.com.vn.bms.model.NotificationModel;
import keeto.com.vn.bms.model.User.EditUserModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.activity.BMSChatActivity;
import keeto.com.vn.bms.ui.activity.HomeBMSActivity;
import keeto.com.vn.bms.ui.activity.InfoChatActivity;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.ui.activity.ReportDetailActivity;
import keeto.com.vn.bms.ui.activity.ViewMoreMsgActivity;
import keeto.com.vn.bms.ui.activity.ViewMoreNotifyActivity;
import keeto.com.vn.bms.ui.adapter.InfoMessegerAdapter;
import keeto.com.vn.bms.ui.adapter.InfoNotifyAdapter;
import keeto.com.vn.bms.ui.adapter.InfoReportAdapter;
import keeto.com.vn.bms.ui.widget.DialogDeleteMsg;
import keeto.com.vn.bms.ui.widget.DialogDeleteNotify;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;
import keeto.com.vn.bms.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.telephony.PhoneNumberUtils;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class InfoBMSHomeFragment extends BaseFragment implements OnClickListener{

	private final int PAGE_LIMIT_INFO = 10;
	private int PageNumber = 1;

	private TextView tvUsernameLogin;
	private EditText edtFullname;
	private ImageButton btnClearFullname;
	private EditText edtUserPhone;
	private ImageButton btnClearUserPhone;
	private EditText edtUserEmail;
	private ImageButton btnClearUserEmail;
	private TextView tvUserPermission;
	private Button btnSave;

	private LinearLayout llNotify;
	private TextView tvInfoNotify;
	private Button btnClearNotify;
	private ListView listViewNotify;
	private Button btnNotifyViewmore;

	private LinearLayout llMsg;
	private TextView tvInfoMesseger;
	private ListView listViewMesseger;

	private LinearLayout llReport;
	private Button btnMessegerViewmore;
	private ListView listViewReport;
	private static int SumNotify = 0;
	private static int SumMsg =0;
	private static int SumNotifyNew = 0;
	private HomeBMSActivity mActivity;
	private TabPageIndicator HomeTabPageIndicator = null;


	/*
	 * userInfo
	 */
	private boolean isEdit = false;

	/*
	 * Notification
	 */
	private ArrayList<NotificationModel> arrNotify = new  ArrayList<NotificationModel>();

	private InfoNotifyAdapter notifyAdapter;  
	private StringBuilder builder;

	/*
	 * Messeger
	 */
	private int typeofMsg=1;
	private ArrayList<InfoMessegerModel> arrMsg = new ArrayList<InfoMessegerModel>();
	private InfoMessegerAdapter messegerAdapter;

	private ArrayList<InfoReportModel> reportModels = new ArrayList<InfoReportModel>();
	private InfoReportAdapter reportAdapter;	

	private int mType;

	private InfoBMSHomeListener mListener = new InfoBMSHomeListener();
	private BMSSharedPreferences preferences;

	public static InfoBMSHomeFragment newInstance(){
		InfoBMSHomeFragment fragment = new InfoBMSHomeFragment();
		Bundle bundle = new Bundle();
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.info_home_fragment_layout, container,false);
		preferences = new BMSSharedPreferences(getActivity());
		mType = preferences.getUserPermission(getActivity());
		mActivity = (HomeBMSActivity) getActivity();
		HomeTabPageIndicator = mActivity.getTabPageIndicator();
		initViewComponent(v);
		initDataReport();
		this.setNumbernotify("" + 5);
		return v;
	}

	private void initViewComponent(View v){
		tvUsernameLogin = (TextView)v.findViewById(R.id.tv_username_login);
		edtFullname = (EditText)v.findViewById(R.id.edt_fullname);
		btnClearFullname = (ImageButton)v.findViewById(R.id.btn_clear_fullname);
		edtUserPhone = (EditText)v.findViewById(R.id.edt_user_phone);
		btnClearUserPhone = (ImageButton)v.findViewById(R.id.btn_clear_user_phone);
		edtUserEmail = (EditText)v.findViewById(R.id.edt_user_email);
		btnClearUserEmail = (ImageButton)v.findViewById(R.id.btn_clear_user_email);
		tvUserPermission = (TextView)v.findViewById(R.id.tv_user_permission);
		btnSave = (Button)v.findViewById(R.id.btn_save_user_info);

		llNotify = (LinearLayout)v.findViewById(R.id.ll_info_notify);
		tvInfoNotify = (TextView)v.findViewById(R.id.tv_info_notify);
		btnClearNotify = (Button)v.findViewById(R.id.btn_clear_info_notify);
		listViewNotify = (ListView)v.findViewById(R.id.listView_info_notify);
		btnNotifyViewmore = (Button)v.findViewById(R.id.btn_info_notify_viewmore);

		llMsg = (LinearLayout)v.findViewById(R.id.ll_info_msg);
		tvInfoMesseger = (TextView)v.findViewById(R.id.tv_info_messeger);
		listViewMesseger = (ListView)v.findViewById(R.id.listView_info_messeger);
		llReport = (LinearLayout)v.findViewById(R.id.ll_info_report);
		btnMessegerViewmore = (Button)v.findViewById(R.id.btn_info_messeger_viewmore);
		listViewReport = (ListView)v.findViewById(R.id.listView_info_report);
		llReport.setVisibility(View.VISIBLE);

		btnClearFullname.setVisibility(View.GONE);
		btnClearUserPhone.setVisibility(View.GONE);
		btnClearUserEmail.setVisibility(View.GONE);
		btnClearFullname.setOnClickListener(this);
		btnClearUserPhone.setOnClickListener(this);
		btnClearUserEmail.setOnClickListener(this);

		btnSave.setOnClickListener(this);
		btnClearNotify.setOnClickListener(this);
		btnNotifyViewmore.setOnClickListener(this);
		btnMessegerViewmore.setOnClickListener(this);
		disableEditUser(false);
		listViewMesseger.setOnItemClickListener(itemMsgClickListener);

		/*
		 * Create Notify Adapter
		 */
		llNotify.setVisibility(View.VISIBLE);
		arrNotify.clear();
		notifyAdapter = new InfoNotifyAdapter(getActivity(), arrNotify, mListener);
		listViewNotify.setAdapter(notifyAdapter);
		notifyAdapter.notifyDataSetChanged();

		notifyAdapter.notifyDataSetChanged();
		Utils.setListViewHeightBasedOnChildrenforList(getActivity(), listViewNotify);

		/*
		 * Create Msg Adapter
		 */
		arrMsg.clear();
		messegerAdapter = new InfoMessegerAdapter(getActivity(), arrMsg);
		listViewMesseger.setAdapter(messegerAdapter);
		messegerAdapter.notifyDataSetChanged();
		Utils.setListViewHeightBasedOnChildrenforList(getActivity(), listViewMesseger);
		listViewMesseger.setOnItemLongClickListener(itemMsgLongClick);

		edtFullname.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				if (s.length() >0 && edtFullname.isEnabled()){
					btnClearFullname.setVisibility(View.VISIBLE);
				}else{
					btnClearFullname.setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		edtUserPhone.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				if (s.length() >0 && edtUserPhone.isEnabled()){
					btnClearUserPhone.setVisibility(View.VISIBLE);
				}else{
					btnClearUserPhone.setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		edtUserEmail.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				if (s.length() >0 && edtUserEmail.isEnabled()){
					btnClearUserEmail.setVisibility(View.VISIBLE);
				}else{
					btnClearUserEmail.setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void updateDataUI(){
		BMSCore.getInstance(getActivity()).taskGetAndroidUserInfo(preferences.getUserAuthKey(getActivity()), mListener);

		if ((preferences.getUserPermission(getActivity()) != IBMSConfig.KEY_TYPE_LEADERSHIP)){
			BMSCore.getInstance(getActivity()).taskGetListNotify(preferences.getUserAuthKey(getActivity()), PAGE_LIMIT_INFO, PageNumber, mListener);

		}

		if ((preferences.getUserPermission(getActivity()) != IBMSConfig.KEY_TYPE_MARKETING) && (preferences.getUserPermission(getActivity()) != IBMSConfig.KEY_TYPE_TECHNICIAL )){
			llMsg.setVisibility(View.VISIBLE);
			BMSCore.getInstance(getActivity()).taskGetListMesseger(typeofMsg, PAGE_LIMIT_INFO, PageNumber, preferences.getUserAuthKey(getActivity()), mListener);
		}else{
			llMsg.setVisibility(View.GONE);
		}

	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updateDataUI();
	}
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private void disableEditUser(boolean isdisable){
		edtFullname.setEnabled(isdisable);
		edtUserEmail.setEnabled(isdisable);
		edtUserPhone.setEnabled(isdisable);
		btnClearFullname.setVisibility(View.INVISIBLE);
		btnClearUserPhone.setVisibility(View.INVISIBLE);
		btnClearUserEmail.setVisibility(View.INVISIBLE);
	}

	private void editUserInfo(){

		EditUserModel model = new EditUserModel();
		BMSUserModel userModel = BMSManager.getInstance(getActivity()).getUserInfo();
		if (!edtFullname.getText().toString().equals("") && !edtFullname.getText().toString().equals(userModel.getFullName())){
			model.setFullname(edtFullname.getText().toString());
		}
		if (!edtUserPhone.getText().toString().equals("")&& !edtUserPhone.getText().toString().equals(userModel.getUserPhone())){
			String numberphone = edtUserPhone.getText().toString();
			char[] charphone = numberphone.toCharArray();
			model.setPhone(edtUserPhone.getText().toString());

			for(int i =0;i<charphone.length;i++){
				if (!PhoneNumberUtils.isISODigit(charphone[i])){
					Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.info_error_phone), Toast.LENGTH_SHORT).show();
					model.setPhone(null);
					break;
				}
			}

		}
		if (!edtUserEmail.getText().toString().equals("") && !edtUserEmail.getText().toString().equals(userModel.getUserEmail()) ){
			String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
			if (edtUserEmail.getText().toString().matches(EMAIL_REGEX)){
				model.setEmail(edtUserEmail.getText().toString());
			}else{
				Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.info_error_email), Toast.LENGTH_SHORT).show();
			}
		}
		if (model.getFullname() != null || model.getPhone()!= null || model.getEmail()!= null){
			BMSCore.getInstance(getActivity()).taskEditUserInfo(model, preferences.getUserAuthKey(getActivity()), mListener);
		}
	}
	private void updateTitleTab(){
		SumNotifyNew = SumMsg + SumNotify;
		InfoBMSHomeFragment.this.setNumbernotify(SumNotifyNew +"");
		if (HomeTabPageIndicator != null){
			HomeTabPageIndicator.notifyDataSetChanged();
		}
	}

	private void initDataReport(){

		switch (mType) {
		case IBMSConfig.KEY_TYPE_CUSTOMER:

			break;
		case IBMSConfig.KEY_TYPE_ADMIN:

			break;
		case IBMSConfig.KEY_TYPE_DOCTOR:

			break;
		case IBMSConfig.KEY_TYPE_MARKETING:

			break;
		case IBMSConfig.KEY_TYPE_TECHNICIAL:

			break;
		case IBMSConfig.KEY_TYPE_LEADERSHIP:
			llNotify.setVisibility(View.GONE);
			reportModels.add(new InfoReportModel(1,getActivity().getString(R.string.info_report_by_hospital),IBMSConfig.KEY_REPORT_BY_HOSPITAL));
			reportModels.add(new InfoReportModel(2,getActivity().getString(R.string.info_report_by_doctor),IBMSConfig.KEY_REPORT_BY_DOCTOR));
			reportModels.add(new InfoReportModel(3,getActivity().getString(R.string.info_report_sum_by_hospital),IBMSConfig.KEY_REPORT_COMPARE_SUM_BY_HOSPITAL));
			reportModels.add(new InfoReportModel(4,getActivity().getString(R.string.info_report_imcome_by_hospital),IBMSConfig.KEY_REPORT_COMPARE_IMCOME_BY_HOSPITAL));
			reportModels.add(new InfoReportModel(5,getActivity().getString(R.string.info_report_by_all),IBMSConfig.KEY_REPORT_COMPARE_BY_ALL));
			break;

		default:
			break;
		}

		if (reportModels.size() >0){
			reportAdapter = new InfoReportAdapter(getActivity(), reportModels);
			listViewReport.setAdapter(reportAdapter);
			Utils.setListViewHeightBasedOnChildrenforList(getActivity(), listViewReport);
			listViewReport.setOnItemClickListener(itemReportClickListener);
			llReport.setVisibility(View.VISIBLE);
		}else{
			llReport.setVisibility(View.GONE);
		}

	}

	private OnItemLongClickListener itemMsgLongClick = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			DialogDeleteMsg confim = new DialogDeleteMsg(getActivity(), mListener, arrMsg.get(position).getMsgID());
			confim.show();
			return false;
		}
	};

	private OnItemClickListener itemMsgClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			BMSCore.getInstance(getActivity()).taskSetReadMessage(arrMsg.get(position).getMsgID(), preferences.getUserAuthKey(getActivity()), mListener);
//			InfoChatActivity.actionlunch(getActivity(), arrMsg.get(position));
			BMSChatActivity.actionLaunch(getActivity());
		}
	};

	private OnItemClickListener itemReportClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			ReportDetailActivity.actionLaunch(getActivity(), reportModels.get(position));
		}
	};



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_clear_fullname:
			edtFullname.setText("");
			break;
		case R.id.btn_clear_user_phone:
			edtUserPhone.setText("");
			break;
		case R.id.btn_clear_user_email:
			edtUserEmail.setText("");
			break;
		case R.id.btn_save_user_info:
			if (isEdit){
				isEdit = false;
				disableEditUser(isEdit);
				btnSave.setText(this.getResources().getString(R.string.btn_edit_info));
				editUserInfo();

			}else{
				isEdit = true;
				disableEditUser(isEdit);
				btnSave.setText(this.getResources().getString(R.string.btn_save_info));

			}
			break;
		case R.id.btn_clear_info_notify:
			deleteNofity();
			break;
		case R.id.btn_info_notify_viewmore:
			ViewMoreNotifyActivity.actionLaunch(getActivity());
			break;
		case R.id.btn_info_messeger_viewmore:
			ViewMoreMsgActivity.actionLaunch(getActivity());
			break;

		default:
			break;
		}
	}
	private void deleteNofity(){
		builder = new StringBuilder();
		builder.append("[");
		for(int i=0;i<arrNotify.size();i++){
			NotificationModel model =  arrNotify.get(i);
			if (model.isChecked() == true){
				builder.append("\"");
				builder.append( model.getNotifyId());
				builder.append("\",");
			}

		}
		String ListId = builder.toString().substring(0,builder.length()-1) +"]";
		
		if (arrNotify.size()>0){
			DialogDeleteNotify deleteNotify = new DialogDeleteNotify(getActivity(), mListener, ListId);
			deleteNotify.show();
		}
	}

	public class InfoBMSHomeListener extends BMSListener{
		@Override
		public void finishGetUserInfo(final int returnCode, final BMSUserModel userModel) {
			// TODO Auto-generated method stub
			super.finishGetUserInfo(returnCode, userModel);
			if (getActivity() != null)	getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK && userModel != null){
						tvUsernameLogin.setText(userModel.getUserName());
						edtFullname.setText(userModel.getFullName());
						edtUserPhone.setText(userModel.getUserPhone());
						edtUserEmail.setText(userModel.getUserEmail());
						tvUserPermission.setText(userModel.getGroupName());
						BMSManager.getInstance(getActivity()).inputUserAccount(userModel);

					}
				}
			});
		}

		@Override
		public void finishEditUserInfo(int returnCode, final String resultMsg) {
			// TODO Auto-generated method stub
			super.finishEditUserInfo(returnCode, resultMsg);
			if (getActivity() != null)	getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.update_sussess), Toast.LENGTH_SHORT).show();
					updateDataUI();
				}
			});
		}

		@Override
		public void finishGetListNotification(final int returnCode, int pageCount,
				final ArrayList<NotificationModel> arrayList) {
			// TODO Auto-generated method stub
			super.finishGetListNotification(returnCode, pageCount, arrayList);
		if (getActivity() != null)	getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK && arrayList != null){

						arrNotify.clear();
						SumNotify =0;
						if (arrayList.size() >3){
							for(int i=0;i<3;i++){
								arrNotify.add(arrayList.get(i));
							}
							btnNotifyViewmore.setVisibility(View.VISIBLE);
						}else{
							arrNotify.addAll(arrayList);
							btnNotifyViewmore.setVisibility(View.GONE);
						}
						notifyAdapter.notifyDataSetChanged();
						for( NotificationModel model : arrayList ){
							if (!model.isRead()){
								SumNotify+=1;
							}
						}
						if (SumNotify != 0 && tvInfoNotify != null){
							tvInfoNotify.setText(getActivity().getResources().getString(R.string.info_notify) +" ("+ Html.fromHtml( "<font color="+ getActivity().getResources().getColor(R.color.orange)+ ">" + SumNotify +"</font>")+")");
						}else if (tvInfoNotify != null){
							tvInfoNotify.setText(getActivity().getResources().getString(R.string.info_notify));
						}
						Utils.setListViewHeightBasedOnChildrenforList(getActivity(), listViewNotify);
					}else{
						btnNotifyViewmore.setVisibility(View.GONE);
						Utils.setListViewHeightBasedOnChildrenforList(getActivity(), listViewNotify);
					}
//					notifyAdapter.getCount() > 0 && 
					if (mType != IBMSConfig.KEY_TYPE_LEADERSHIP ){
						llNotify.setVisibility(View.VISIBLE);
					}else{
						llNotify.setVisibility(View.GONE);
					}
					updateTitleTab();


				}
			});
		}

		@Override
		public void finishGetListMesseger(final int returnCode, int pageCount,
				final ArrayList<InfoMessegerModel> ListMsg) {
			// TODO Auto-generated method stub
			super.finishGetListMesseger(returnCode, pageCount, ListMsg);
			if (getActivity() != null)	getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK && ListMsg != null){
						arrMsg.clear();
						SumMsg = 0;
						llMsg.setVisibility(View.VISIBLE);

						if (ListMsg.size() >3){
							btnMessegerViewmore.setVisibility(View.VISIBLE);
							for(int i=0;i<3;i++){
								arrMsg.add(ListMsg.get(i));
							}
						}else{
							btnMessegerViewmore.setVisibility(View.GONE);
							arrMsg.addAll(ListMsg);
						}
						for(InfoMessegerModel messegerModel : ListMsg){
							if (!messegerModel.isRead()){
								SumMsg+=1;
							}
						}
						if (SumMsg!= 0 && tvInfoMesseger!= null){
							tvInfoMesseger.setText(getActivity().getResources().getString(R.string.info_messeger)+ " (" + SumMsg + ")");
						}else if (tvInfoMesseger != null){
							tvInfoMesseger.setText(getActivity().getResources().getString(R.string.info_messeger));
						}
						messegerAdapter.notifyDataSetChanged();

						updateTitleTab();
						Utils.setListViewHeightBasedOnChildrenforList(getActivity(), listViewMesseger);
					}else{
						llMsg.setVisibility(View.GONE);
						btnMessegerViewmore.setVisibility(View.GONE);
					}
					updateTitleTab();

				}
			});
		}

		@Override
		public void finishSendReadNotify(final int returnCode, final String resultMsg) {
			// TODO Auto-generated method stub
			super.finishSendReadNotify(returnCode, resultMsg);
			if (getActivity() != null)	getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK && resultMsg != null){
						BMSCore.getInstance(getActivity()).taskGetListNotify(preferences.getUserAuthKey(getActivity()), PAGE_LIMIT_INFO, PageNumber, mListener);
					}
				}
			});
		}

		@Override
		public void finishSendReadMessage(final int returnCode, final String resultMsg) {
			// TODO Auto-generated method stub
			super.finishSendReadMessage(returnCode, resultMsg);
			if (getActivity() != null)	getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK){
						if ((preferences.getUserPermission(getActivity()) != IBMSConfig.KEY_TYPE_MARKETING) && (preferences.getUserPermission(getActivity()) != IBMSConfig.KEY_TYPE_TECHNICIAL )){
							llMsg.setVisibility(View.VISIBLE);
							BMSCore.getInstance(getActivity()).taskGetListMesseger(typeofMsg, PAGE_LIMIT_INFO, PageNumber, preferences.getUserAuthKey(getActivity()), mListener);
						}else{
							llMsg.setVisibility(View.GONE);
						}			
					}
				}
			});
		}

		@Override
		public void finishSendDeleteNotify(final int returnCode, final String resultMsg) {
			// TODO Auto-generated method stub
			super.finishSendDeleteNotify(returnCode, resultMsg);

			if (getActivity() != null)		getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK ){
						arrNotify.clear();
						notifyAdapter.notifyDataSetChanged();
						PageNumber = 1;
						BMSCore.getInstance(getActivity()).taskGetListNotify(preferences.getUserAuthKey(getActivity()), PAGE_LIMIT_INFO, PageNumber, mListener);

					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(getActivity(),getActivity().getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					} 
				}
			});

		}

		@Override
		public void finishSendDeleteMessager(final int returnCode, final String resultMsg) {
			// TODO Auto-generated method stub
			super.finishSendDeleteMessager(returnCode, resultMsg);
			if (getActivity() != null)		getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK){
						Utils.setListViewHeightBasedOnChildrenforList(getActivity(), listViewMesseger);
						BMSCore.getInstance(getActivity()).taskGetListMesseger(typeofMsg, PAGE_LIMIT_INFO, PageNumber, preferences.getUserAuthKey(getActivity()), mListener);

					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}

	}


}
